package com.weipai.springboot.module.controller;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.weipai.springboot.enums.RetCode;
import com.weipai.springboot.model.*;
import com.weipai.springboot.model.api.PageResult;
import com.weipai.springboot.model.api.Response;
import com.weipai.springboot.module.dao.ShopCollectionDao;
import com.weipai.springboot.module.dao.ShopDao;
import com.weipai.springboot.module.mapper.ShopCollectionMapper;
import com.weipai.springboot.module.service.CollectionService;
import com.weipai.springboot.module.service.GoodsService;
import com.weipai.springboot.module.service.ShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by ${lyh} on 2018/8/27.
 * 收藏模块
 */

@RestController
@RequestMapping("/Collection/")
public class CollectionController {

    @Autowired
    CollectionService collectionService;
    @Autowired
    GoodsService goodsService;
    @Autowired
    ShopService shopService;
    @Autowired
    ShopDao shopDao;
    @Autowired
    ShopCollectionMapper shopCollectionMapper;

    /*
        *   商品收藏列表
        * @param page 页码
        * @param size 每页条数
        */
    @GetMapping("/goodscollectionlist/{page}/{size}/{customerId}/{draw}")
    public Response list( @PathVariable("page") Integer page,
                         @PathVariable("size")Integer size,
                          @PathVariable("customerId") String customerId,
                          @PathVariable("draw")String draw
                          ){
        Response response = new Response();
        page = (page-1) * size;
        List<GoodsCollection> list = collectionService.selectAllByGoodsCollection(customerId,page,size);
        if(list.size()>0){
            Integer totalNum = collectionService.countByAll(customerId);
            PageResult<GoodsCollection> pageList = new PageResult<GoodsCollection>(list, page, size,totalNum , draw);
            response.put("pageList", pageList);
            response.setCode(RetCode.SUCCESS);
        }else{
            response.setCode(RetCode.NO_DATA);
        }
        return response;
    }

    /**
     *  添加商品到   我的收藏
     * @param customerId：客户id
     * @param goodsId：  商品id
     * @return
     */
    @ResponseBody
    @RequestMapping("/goods/save")
    public Response saveGoodsCollection(String customerId, String goodsId){
        return collectionService.saveGoodsCollection(customerId, goodsId);
    }

    /*
         商品详情 判断是否收藏此商品
     */
    @RequestMapping("/goods/varGoodsCollection")
    public Response varGoodsCollection(String customerId, String goodsId){
        return collectionService.varGoodsCollection(customerId, goodsId);
    }


    /**
     * 取消   单个商品收藏
     * @return
     */
    @ResponseBody
    @RequestMapping("/goodscollection/deleteById")
    public Response deleteById(String customerId, String goodsId){
        return collectionService.deleteById(customerId,goodsId);
    }


    /**
     * 多个  取消收藏
     * @return
     */
    @ResponseBody
    @RequestMapping("/goodscollection/deleteByIdMore")
    public Response deleteByIdMore(String customerId, String[] goodsIds){
        return collectionService.deleteByIdMore(customerId,goodsIds);
    }



    /*
        一键清空 商品收藏
     */
    @ResponseBody
    @RequestMapping("/goodscollection/deleteByAll")
    public Response deleteByAll(String customerId){
        return collectionService.deleteByAll(customerId);
    }


    /**
     * 批量插入  商品收藏
     * @return
     */
    @ResponseBody
    @RequestMapping("/shopCollection/allAdd")
    public Response allAdd(String customerId,String[] goodsId){
        return collectionService.allAdd(customerId,goodsId);
    }



    /*
   *   商铺收藏列表---------------------------------------------------------------------------------------------------------------------------
   * @param page 页码
   * @param size 每页条数
   */
    @GetMapping("/shopcollectionlist/{page}/{size}/{customerId}/{draw}")
    public Response shopcollectionlist(
            @PathVariable("page") Integer page,
            @PathVariable("size") Integer size,
            @PathVariable("draw")String draw,
            @PathVariable("customerId") String customerId){
        Response response = new Response();
        page = (page-1) * size;
        List<ShopCollection> list = collectionService.selectAllByShopCollection(customerId,page,size);

        List<Shop> shopList = new ArrayList<>();
        if(list.size()>0){
            for (ShopCollection shopCollection:list ) {
                //通过shopid 搜索 商铺信息
                Shop shop = shopDao.getShopById(shopCollection.getShopId());
                if(shop!=null){
                    //通过shopid  搜索 商品数量
                    int goodsNum = goodsService.countByShop(shopCollection.getShopId());
                    //一个商铺有 多少人收藏
                    int shopCollectionPeople = shopCollectionMapper.countByShopId(shopCollection.getShopId());
                    //商铺 有商品数数量
                    shop.setGoodsNumber(goodsNum);
                    shop.setCollectionNumber(shopCollectionPeople);
                    shopList.add(shop);
//               PageInfo<Shop> pageInfo = new PageInfo<>(shopList);
                }
            }
            Integer num =shopList.size();
            if(shopList.size()>0){
                PageResult<Shop> pageList = new PageResult<Shop>(shopList, page, size,num ,draw);
                response.put("pageList", pageList);
                response.setCode(RetCode.SUCCESS);
            }else{
                response.setCode(RetCode.NO_DATA);
            }
        }else{
            response.setCode(RetCode.NO_DATA);
        }
        return response;
    }


    /**
     *   添加店铺到   我的收藏
     * @param customerId：客户id
     * @param shopId：  店铺id
     * @return
     */
    @ResponseBody
    @RequestMapping("/shop/save")
    public Response saveShopCollection(String customerId, String shopId){
        return collectionService.saveShopCollection(customerId, shopId);
    }

    /**
     * 取消 单个店铺收藏
     * @return
     */
    @ResponseBody
    @RequestMapping("/shopscollection/deleteById")
    public Response deleteShopById(String customerId, String shopId){
        return collectionService.deleteShopById(customerId,shopId);
    }


    /*
        一键清空 商店收藏
     */
    @ResponseBody
    @RequestMapping("/shop/deleteByAllShop")
    public Response deleteByAllShop(String customerId){
        return collectionService.deleteByAllShop(customerId);
    }




}
